{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LGBM → PMML \n",
    "\n",
    "### Exporter: LGBMRegressor\n",
    "\n",
    "### Data Set used: Auto\n",
    "\n",
    "\n",
    "### **STEPS**: \n",
    "- Build the Pipeline with preprocessing \n",
    "- Build PMML using Nyoka exporter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pre-processing, Model building (using pipeline) for Auto data set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:23:34.023366Z",
     "start_time": "2018-08-13T17:23:33.043866Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "     steps=[('mapper', DataFrameMapper(default=False, df_out=False,\n",
       "        features=[('car name', CountVectorizer(analyzer='word', binary=False, decode_error='strict',\n",
       "        dtype=<class 'numpy.int64'>, encoding='utf-8', input='content',\n",
       "        lowercase=True, max_df=1.0, max_features=None, min_df=1,....0, reg_lambda=0.0, silent=True,\n",
       "       subsample=1.0, subsample_for_bin=200000, subsample_freq=0))])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn import datasets\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from lightgbm import LGBMRegressor,LGBMClassifier\n",
    "from sklearn_pandas import DataFrameMapper\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "auto = pd.read_csv('auto-mpg.csv')\n",
    "X = auto.drop(['mpg'], axis=1)\n",
    "y = auto['mpg']\n",
    "\n",
    "feature_names = [name for name in auto.columns if name not in ('mpg')]\n",
    "\n",
    "target_name='mpg'\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=101)\n",
    "pipeline_obj = Pipeline([\n",
    "    ('mapper', DataFrameMapper([\n",
    "        ('car name', CountVectorizer()),\n",
    "        (['displacement'],[StandardScaler()]) \n",
    "    ])),\n",
    "    ('lgbmr',LGBMRegressor(n_estimators=22))\n",
    "])\n",
    "pipeline_obj.fit(x_train,y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Export the Pipeline object into PMML using the Nyoka package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:23:36.956866Z",
     "start_time": "2018-08-13T17:23:36.319366Z"
    }
   },
   "outputs": [],
   "source": [
    "from nyoka import lgb_to_pmml\n",
    "lgb_to_pmml(pipeline_obj,feature_names,target_name,\"lgbmr_pmml_preprocess.pmml\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
